$(function () {
    if (window.parent === window.self) {
        max_width = window.document.documentElement.clientWidth;
        max_height = window.document.documentElement.clientHeight;
    } else {
        var panel = $(".tabs-panels", window.parent.document);

        max_width = panel.width;
        max_height = panel.height;
    }

    $('#listGrid').datagrid({
        toolbar: '#toolbar',
        title: '按钮列表',
        iconCls: 'icon icon-chart_organisation',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        url: '/sys/button/list',
        queryParams: {
            _token: $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id: $('meta[name="curr_menu_id"]').attr('content')
        },
        columns: [[
            {title: 'id', field: 'id', hidden: true,width: 60 },
            {title: '名称', field: 'name', width: 130},
            {title: 'code', field: 'code', width: 130},
            {title: '图标样式', field: 'icon_style', width: 130,formatter:function(value,row,index){
                return '<i class="'+value+'" style="width: 16px;height: 16px;display: inline-block;"></i>'
            }},
            {title: '方法名', field: 'func', width: 130},
            {title: '创建时间', field: 'created_at', width: 150},
            {title: '更新时间', field: 'updated_at', width: 150},
            {title: '是否生效', field: 'is_active', width: 130,formatter:function(value,row,index){
                 if(value=='1'){
                    return '生效'
                }else{
                     return '无效'
                 }
            }},
        ]],
        onLoadSuccess: function (data) {
            if (data === null || data.total === 0) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暂无数据</h1></td></tr>');
            }
        }
    });
})

function openAdd() {
    $('#form_dialog').dialog({
        title: '添加按钮信息',
        width: 500,
        height: 350,
        closed: false,
        cache: false,
        draggable:false,
        href: '/sys/button/add',
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit() {
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行", "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑按钮信息',
        width: 500,
        height: 350,
        closed: false,
        cache: false,
        draggable:false,
        href: '/sys/button/edit?button_id=' + rows.id,
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save() {
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: '/sys/button/save',
        type: 'post',
        data: $('#info-form').serialize(),
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $('#listGrid').datagrid("reload")
                $('#form_dialog').dialog('close');
            } else {
                $.messager.alert("提示信息", data.data, "error");
            }
        }
    });
}

function reload() {
    $('#listGrid').datagrid("reload");
}

function  openIcon() {
    $('#icon_dialog').dialog({
        title: '选择图标',
        width: 400,
        height: 200,
        closed: false,
        cache: true,
        href: '/css/iconlist.html?v=' + Math.random() ,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function () {
                $('#icon_style').textbox({value:$.trim($('.l-btn-icon-left').attr('data-selected-class'))});
                $('#icon_dialog').dialog('close');
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#icon_dialog').dialog('close');
            }
        }]
    });
}
